<template>
  <editForm
    :formConfig="basicConfig"
    ref="basicForm"
    :formData="formData"
  ></editForm>
</template>

<script>
export default {
  props: {
    allInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    status: {
      type: Number,
    },
  },
  data() {
    return {
      minTime: null,
      maxTime: null,
      basicConfig: {
        labelWidth: "160px",
        itemArr: [
          {
            label: "实际结束时间",
            prop: "factEndTime",
            type: "datetime",
            required: true,
            disabled: false,
            span: 8,
            rules: {
              trigger: "blur",
              validator: (rule, value, callback) => {
                this.checkTime(rule, value, callback);
              },
            },
            pickerOptions: {
              disabledDate: (time) => {
                return (
                  time.getTime() < this.minTime ||
                  time.getTime() >= this.maxTime
                );
              },
            },
            methods: (val) => {
              let times =
                new Date(val).getTime() -
                new Date(this.allInfo.applicationTime.factStartTime).getTime();
              let hours = (times / (60 * 60 * 1000)).toFixed(2);
              this.$refs.basicForm.setFormItem("timeLength", hours);
            },
          },
          {
            label: "实际检修时长(h)",
            prop: "timeLength",
            type: "text",
            required: true,
            disabled: true,
            span: 8,
          },
          {
            label: "备注人",
            prop: "endCheckerName",
            type: "text",
            required: false,
            disabled: true,
            span: 8,
          },
          {
            label: "备注内容",
            prop: "endDesc",
            type: "textarea",
            required: true,
            span: 24,
            rows: 4,
          },
        ],
      },
      formData: {},
    };
  },
  created() {
    if (this.status > 3) {
      this.basicConfig.itemArr.map((i) => (i.disabled = true));
    }
  },
  watch: {
    allInfo: {
      handler(val) {
        this.init(val);
      },
      deep: true,
    },
    status: {
      handler(val) {
        if (val > 3) {
          this.basicConfig.itemArr.map((i) => (i.disabled = true));
        }
      },
    },
  },
  methods: {
    init(val) {
      let data = {};
      if (val.applicationTime) {
        data = JSON.parse(JSON.stringify(val.applicationTime));
        this.minTime = new Date(
          `${this.allInfo.applicationTime.factStartTime.split(" ")[0]} 00:00`
        ).getTime();
        this.maxTime = new Date(
          `${this.allInfo.applicationCheck.endTime.split(" ")[0]} 24:00`
        ).getTime();
        let times =
          new Date(data.factEndTime).getTime() -
          new Date(data.factStartTime).getTime();
        let hours = times / (60 * 60 * 1000);
        if (data.factEndTime) {
          data.timeLength = hours.toFixed(2);
        }
      }
      // if (val.applicationTime && !val.applicationTime.factEndTime) {
      //   data.endCheckerName = this.$store.getters.userInfo.name;
      // }
      this.formData = data;
    },
    //校验申请时间
    checkTime(rule, val, callback) {
      let minTime = new Date(
        this.allInfo.applicationTime.factStartTime
      ).getTime();
      let maxTime = new Date(this.allInfo.applicationCheck.endTime).getTime();
      let time = new Date(val).getTime();
      let message = `请在${this.allInfo.applicationTime.factStartTime}-${this.allInfo.applicationCheck.endTime}内选择`;
      if (minTime > time || time > maxTime) {
        //debugger
        return callback(new Error(message));
      } else {
        callback();
      }
    },
    async getFactEndData() {
      let formData = await this.$refs.basicForm.getFormData();
      if (formData.endCheckerName) {
        formData.endCheckerName = this.$store.getters.userInfo.name;
      }
      return formData;
    },
  },
};
</script>

<style lang="less" scoped></style>
